<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @property CI_DB_active_record $db
 * @property CI_DB_forge $dbforge
 * @property CI_Benchmark $benchmark
 * @property CI_Calendar $calendar
 * @property CI_Cart $cart
 * @property CI_Config $config
 * @property CI_Controller $controller
 * @property CI_Email $email
 * @property CI_Encrypt $encrypt
 * @property CI_Exceptions $exceptions
 * @property CI_Form_validation $form_validation
 * @property CI_Hooks $hooks
 * @property CI_Image_lib $image_lib
 * @property CI_Input $input
 * @property CI_Lang $lang
 * @property CI_Loader $load
 * @property CI_Log $log
 * @property CI_Model $model
 * @property CI_Output $output
 * @property CI_Pagination $pagination
 * @property CI_Parser $parser
 * @property CI_Profiler $profiler
 * @property CI_Router $router
 * @property CI_Session $session
 * @property CI_Sha1 $sha1
 * @property CI_Table $table
 * @property CI_Trackback $trackback
 * @property CI_Typography $typography
 * @property CI_Unit_test $unit_test
 * @property CI_Upload $upload
 * @property CI_URI $uri
 * @property CI_User_agent $agent
 * @property CI_Validation $validation
 * @property CI_Xmlrpc $xmlrpc
 * @property CI_Xmlrpcs $xmlrpcs
 * @property CI_Zip $zip
 * @property CI_Javascript $javascript
 * @property CI_Jquery $jquery
 * @property CI_Utf8 $utf8
 * @property CI_Security $security
 * @property CI_Driver_Library $driver
 * @property CI_Cache $cache
 * @property CI_Ftp $ftp
 *
 *
 *
 *
 * @property metadata_model $metadata_model
 * @property metadata $metadata
 * @property user $user
 * @property user_model $user_model
 * @property language $language
 * @property language_model $language_model
 * @property settings_model $settings_model
 * @property category_model $category_model
 * @property resolution_model $resolution_model
 * @property progress_bar $progress_bar
 * @property wallpaper_model $wallpaper_model
 * @property hashids $hashids
 * @property routes_model $routes_model
 * @property image_workshop $image_workshop
 * @property route_links $route_links
 * @property frontend_helper $frontend_helper
 * @property ads_model $ads_model
 *
 *
 */
class WS_controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        //$this->output->enable_profiler(TRUE);
        if ($this->config->item('set_global_time_zone'))
            @date_default_timezone_set($this->config->item('set_global_time_zone'));
        $this->load->library('user');
        $this->load->helper('url');
        $this->db->cache_dir_ = $this->db->cachedir;

        define('PHPWS_VERSION', 'V'.$this->config->item('current_version'));
    }

    public function  both_class_load()
    {
        $this->load->library(array('image_workshop', 'route_links'));
        if ($this->config->item('enable_captcha'))
            $this->load->helper('recaptchalib');
    }

    public function referer($strpos = false)
    {
        if ($strpos && strpos(strtolower($this->uri->uri_string()), strtolower($strpos)) !== false) {
            return base_url();
        }
        if (isset($_SERVER['HTTP_REFERER']))
            return str_replace('index.php', '', $_SERVER['HTTP_REFERER']);
        return base_url();
    }

    public function template_path()
    {
        return 'themes/' . $this->config->item('frontendTheme') . '/';
    }
}

class wsBackend extends WS_controller
{
    public function __construct()
    {
        parent::__construct();
        $this->db->cache_off();
        if (!$this->user->is_admin(TRUE)) {
            $this->user->logOut();
            if (strtolower(get_class($this)) != 'login') {
                redirect(admin_url('login'));
            }
        }


        $this->tpl->template_path = $this->template_path();

        $this->both_class_load();
        $this->load->add_package_path(APPPATH_RAW . '/themes/' . $this->config->item('backendTheme') . '/');
        $this->config->set_item('is_administrator', true);
    }
}

class wsFrontend extends WS_controller
{
    public function __construct()
    {
        parent::__construct();
        $this->both_class_load();
        $this->load->add_package_path($this->template_path());
        $this->config->set_item('is_administrator', false);
    }
}